package com.ywjt.platformbusiness.store.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseActivity;
import com.ywjt.platformbusiness.store.adapter.StoreAddSkuAdapter;
import com.ywjt.platformbusiness.store.model.SkuListModel;
import com.ywjt.platformbusiness.store.model.SkuModel2;
import com.ywjt.platformbusiness.widget.CenterDialogEdit;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ActivityGoodsSku extends BaseActivity implements View.OnClickListener {
    private RecyclerView rcCommon;
    private StoreAddSkuAdapter typeAdapter;
    private TextView tvSure;
    private List<SkuListModel> data;
    private TextView _barTvRight;
    private int index=0;


    private List<SkuModel2.GoodsSkuVOListDTO>goodsSkuVOList=new ArrayList();

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, ActivityGoodsSku.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_goods_sku;
    }

    List<SkuListModel.values> bean=null;

    @Override
    protected void initView() {
        setTitleText("规格选择");
        _barTvRight=findViewById(R.id._barTvRight);
        index=getIntent().getIntExtra("index",0);
        _barTvRight.setText("完成");
        _barTvRight.setVisibility(View.VISIBLE);
        _barTvRight.setOnClickListener(this::onClick);
        rcCommon=findViewById(R.id.rcCommon);
        tvSure=findViewById(R.id.tvSure);
        tvSure.setOnClickListener(this::onClick);

        data=new ArrayList<>();
        typeAdapter=new StoreAddSkuAdapter(data);
        rcCommon.setAdapter(typeAdapter);
        rcCommon.setLayoutManager(new LinearLayoutManager(ActivityGoodsSku.this));
        typeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.tvAdd:
                        if (data.get(position).getResult()!=null&&data.get(position).getResult().size()>0) {
                           Toast.makeText(ActivityGoodsSku.this,"最多只能添加一个属性值",Toast.LENGTH_SHORT).show();
                           return;
                        }else {
                            CenterDialogEdit dialogEdit=new CenterDialogEdit(ActivityGoodsSku.this,data.get(position).getTitle(),"");
                            dialogEdit.setClicklistener(new CenterDialogEdit.ClickListenerInterface() {
                                @Override
                                public void confirm(String price) {
                                    bean=new ArrayList<>();
                                    SkuListModel.values m=new SkuListModel.values();
                                    m.setTitle(price);
                                    bean.add(m);
                                    data.get(position).setResult(bean);
                                    typeAdapter.notifyItemChanged(position);
                                    dialogEdit.dismiss();

                                }
                            });
                            dialogEdit.show();
                        }
                        break;
                }
            }
        });
    }

    @Override
    protected void initData() { }

    /**
     * 获取要使用的标题栏样式(此函数由子类重写)
     * @return
     */
    @Override
    protected int getTitleBarType() {
        return 0;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvSure:
                //添加
                if (data.size()==3){
                    Toast.makeText(ActivityGoodsSku.this,"商品只能添加3个属性",Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    CenterDialogEdit edit=new CenterDialogEdit(ActivityGoodsSku.this,"属性", "");
                    edit.setClicklistener(new CenterDialogEdit.ClickListenerInterface() {
                        @Override
                        public void confirm(String price) {
                            SkuListModel model=new SkuListModel();
                            model.setTitle(price);
                            data.add(model);
                            typeAdapter.notifyItemInserted(data.size()-1);
                            edit.dismiss();
                        }
                    });
                    edit.show();
                }
                break;
            case R.id._barTvRight:
                if (data.size()>0){
                    computData();
                        Intent intent = new Intent();
                        intent.putExtra("goodsSkuVOList",(Serializable) goodsSkuVOList);
                        intent.putExtra("index",index);
                        setResult(RESULT_OK, intent);
                        finish();
                }else {
                    Toast.makeText(ActivityGoodsSku.this,"请先添加相关属性以及属性值",Toast.LENGTH_SHORT).show();
                    return;
                }

                break;
        }
    }

    public void computData(){
        for (int i=0;i<data.size();i++){
            if (data.get(i).getResult().size()<1){
                Toast.makeText(ActivityGoodsSku.this,"属性值不能为空",Toast.LENGTH_SHORT).show();
                return;
            }
            SkuModel2.GoodsSkuVOListDTO map=new SkuModel2.GoodsSkuVOListDTO();
            map.setKey(data.get(i).getTitle());
            map.setValue(data.get(i).getResult().get(0).getTitle());
            goodsSkuVOList.add(map);
        }
    }
}